/**
 * @author Javen 
 * @copyright 2018-11-06 17:38:45 javendev@126.com 
 * @description 龙骨动画动态加载测试
 * https://docs.cocos.com/creator/api/zh/classes/ArmatureDisplay.html
 */

cc.Class({
    extends: cc.Component,

    properties: {
        dragonNode: {
            default: null,
            type: cc.Node,
            tooltip: "龙骨节点"
        },
        _left: true,
        _currentAnimation: "null",
    },

    // onLoad () {},
    toBack() {
        cc.director.loadScene("Scene");
    },
    start() {
        if (this.dragonNode.active) {
            //获取 ArmatureDisplay
            this._armatureDisPlay = this.dragonNode.getComponent(dragonBones.ArmatureDisplay);
            //添加动画监听
            this._armatureDisPlay.addEventListener(dragonBones.EventObject.FADE_IN_COMPLETE, this._animationEventHandler, this);
            this._armatureDisPlay.addEventListener(dragonBones.EventObject.FADE_OUT_COMPLETE, this._animationEventHandler, this);
        } else {
            //动态加载
            cc.loader.loadResDir('dragon', function (err, assets) {
                if (err) {
                    return;
                }
                if (assets.length <= 0) {
                    return;
                }
                var newHero = new cc.Node();
                this.node.addChild(newHero);
                newHero.setPosition(cc.v2(0, 0));
                newHero.setScale(0.5, 0.5);
                this._armatureDisPlay = newHero.addComponent(dragonBones.ArmatureDisplay);

                for (let i in assets) {
                    if (assets[i] instanceof dragonBones.DragonBonesAsset) {
                        this._armatureDisPlay.dragonAsset = assets[i];
                    }
                    if (assets[i] instanceof dragonBones.DragonBonesAtlasAsset) {
                        this._armatureDisPlay.dragonAtlasAsset = assets[i];
                    }
                }

                //添加动画监听
                this._armatureDisPlay.addEventListener(dragonBones.EventObject.COMPLETE, this._animationEventHandler, this);
                this._armatureDisPlay.armatureName = 'Swordsman';
                this._armatureDisPlay.playAnimation('walk');
                this._currentAnimation = "walk";
            }.bind(this));
        }
    },

    toAttack() {
        if (this._left) {
            this._left = false;
            this.rightAttack();
        } else {
            this._left = true;
            this.leftAttack();
        }
    },

    leftAttack() {
        cc.log("左攻击..");
        if (this.dragonNode.active) {
            this._armatureDisPlay.playAnimation('zk', 1);
        } else {
            this._currentAnimation = "attack1";
            this._armatureDisPlay.playAnimation('attack1', 1);
        }

    },
    rightAttack() {
        cc.log("右攻击..");
        if (this.dragonNode.active) {
            this._armatureDisPlay.playAnimation('yk', 1);
        } else {
            this._currentAnimation = "attack2";
            this._armatureDisPlay.playAnimation('attack2', 1);
        }
    },
    _animationEventHandler(event) {
        if (event.type == dragonBones.EventObject.COMPLETE) {
            cc.log('complete');
            if (this._currentAnimation != "walk") {
                this._armatureDisPlay.playAnimation('walk');
            }
        }
    },
    // update (dt) {},
});